package com.ruoyi.partybuilding.service.impl;

import java.util.List;

import com.ruoyi.partybuilding.common.EQueryHelper;
import com.ruoyi.partybuilding.domain.vo.ReviewStatVo;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.partybuilding.mapper.ReviewUserMapper;
import com.ruoyi.partybuilding.domain.ReviewUser;
import com.ruoyi.partybuilding.service.IReviewUserService;

import javax.annotation.Resource;

/**
 * 评议用户Service业务层处理
 * 
 * @author he-liangliang@outlook.com
 * @date 2024-11-27
 */
@Service
public class ReviewUserServiceImpl implements IReviewUserService 
{
    @Autowired
    private ReviewUserMapper reviewUserMapper;
    @Resource
    protected EQueryHelper queryHelper;

    static final String reviewResultStatSqlTmpl="SELECT review_id,reviewed_user,count(1) userCount,MAX(reviewed_user_name) reviewed_user_name,max(reviewed_user_dept) reviewed_user_dept,max(reviewed_user_dept_name) reviewed_user_dept_name, sum(case WHEN grade=0 then 1 else 0 end) grade0, sum(case WHEN grade=1 then 1 else 0 end) grade1, sum(case WHEN grade=2 then 1 else 0 end) grade2, sum(case WHEN grade=3 then 1 else 0 end) grade3, sum(case WHEN grade=4 then 1 else 0 end) grade4, sum(case WHEN grade=5 then 1 else 0 end) grade5 FROM pb_review_user WHERE 1=1 %s GROUP BY review_id,reviewed_user";

    /** 各等级评价数量，按评议和类型，进行数量统计，
     *
     * @param reviewId
     * @param type 0互评,1自评，2组织，3群众，
     * @return
     */
    public List<ReviewStatVo> reviewResultStat(Long reviewId,Integer type) {
        var append = "";
        if (reviewId != null)
            append = " and review_id=" + reviewId;
        if (type != null)
            append += " and type=" + type;
        var sql = String.format(reviewResultStatSqlTmpl, append);
        return queryHelper.CreateItems(sql).resultPkg().sqlQuery(ReviewStatVo.class);
    }

    /**
     * 查询评议用户
     * 
     * @param id 评议用户主键
     * @return 评议用户
     */
    @Override
    public ReviewUser selectReviewUserById(Long id)
    {
        return reviewUserMapper.selectReviewUserById(id);
    }

    /**
     * 查询评议用户列表
     * 
     * @param reviewUser 评议用户
     * @return 评议用户
     */
    @Override
    public List<ReviewUser> selectReviewUserList(ReviewUser reviewUser)
    {
        return reviewUserMapper.selectReviewUserList(reviewUser);
    }

    /**
     * 新增评议用户
     * 
     * @param reviewUser 评议用户
     * @return 结果
     */
    @Override
    public int insertReviewUser(ReviewUser reviewUser)
    {
        return reviewUserMapper.insertReviewUser(reviewUser);
    }

    /**
     * 修改评议用户
     * 
     * @param reviewUser 评议用户
     * @return 结果
     */
    @Override
    public int updateReviewUser(ReviewUser reviewUser)
    {
        return reviewUserMapper.updateReviewUser(reviewUser);
    }

    /**
     * 批量删除评议用户
     * 
     * @param ids 需要删除的评议用户主键
     * @return 结果
     */
    @Override
    public int deleteReviewUserByIds(Long[] ids)
    {
        return reviewUserMapper.deleteReviewUserByIds(ids);
    }

    /**
     * 删除评议用户信息
     * 
     * @param id 评议用户主键
     * @return 结果
     */
    @Override
    public int deleteReviewUserById(Long id)
    {
        return reviewUserMapper.deleteReviewUserById(id);
    }
}
